package com.meixiang.adapter.moments;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.account.CommunityNewsList;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.flowlayout.CustomFlowAdapter;
import com.meixiang.view.flowlayout.CustomFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/5/7 16:57
 */
public class CommunityNewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context context;

    private List<CommunityNewsList> mList = new ArrayList<>();

    public List<CommunityNewsList> getData(){
        return mList;
    }

    public CommunityNewsAdapter(Context context) {
        this.context = context;
    }


    public void addAll(List<CommunityNewsList> mList) {
        this.mList.addAll(mList);
        this.notifyDataSetChanged();
    }

    public void clearData(){
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_community_news, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;


    }

    @SuppressWarnings("unchecked")
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, pos);
                    // 点击item进入详情界面

                }
            });

        }
        if (mList != null) {
            CommunityNewsList mData = mList.get(position);
            if (mData.getMessageType().equals("3")) {
                ((ViewHolder) holder).mTvContent.setVisibility(View.GONE);
                ((ViewHolder) holder).mIvCommunityPicture.setVisibility(View.VISIBLE);
            }
            ((ViewHolder) holder).mTvOtherUserName.setText(mData.getMemberName());
            ((ViewHolder) holder).mTvContent.setText(mData.getComment());
            ((ViewHolder) holder). mTvDate.setText(mData.getMessageTime());
            GlideHelper.showImage(context, mData.getMemberAvatar(), R.mipmap.default_head_portrait,  ((ViewHolder) holder).mIvUserHeadPortrait);
//            GlideHelper.showImage(context, mData.getCommentImages(), R.mipmap.recommend2,  ((ViewHolder) holder).mIvCommunityPicture);
            final String[] urls = mData.getCommentImages();
            if (urls == null || urls.length == 0) {
                ((ViewHolder) holder).mIvCommunityPicture.setVisibility(View.GONE);
            } else {
                ((ViewHolder) holder).mIvCommunityPicture.setVisibility(View.VISIBLE);
                ((ViewHolder) holder).mIvCommunityPicture.setAdapter(new CustomFlowAdapter(urls) {
                    @Override
                    public View getView(CustomFlowLayout parent, int position, Object o) {
                        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_attention_image, parent, false);
                        ImageView imageView = (ImageView) layout.findViewById(R.id.iv_image);
                        GlideHelper.showImage(context, (String) o, R.mipmap.recommend3, imageView);
                        return layout;
                    }
                });

            }

        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    private OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.iv_user_head_portrait)
        ImageView mIvUserHeadPortrait;
        @Bind(R.id.tv_other_user_name)
        TextView mTvOtherUserName;
        @Bind(R.id.tv_content)
        TextView mTvContent;
        @Bind(R.id.tv_date)
        TextView mTvDate;
        @Bind(R.id.tv_time)
        TextView mTvTime;
        @Bind(R.id.iv_community_picture)
        CustomFlowLayout mIvCommunityPicture;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
